package com.example.onlinetaxi.Thread;

import android.content.Context;
import android.os.Looper;
import android.widget.Toast;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.callback.OrderCallBack.BestOrderCallBack;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.dto.OrderDto.OrderDTO;
import com.example.onlinetaxi.dto.OrderDto.ReceivedOrderDto;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

// 用来显示可接取得订单(点击出车启动线程，不再出车/接收订单时销毁线程)
// 只要有一个订单，就弹出接单对话框
public class DriverReceiveOrderThread {

    // 为该线程初始化并启动
    public static Thread thread = null;

    // 线程终止变量
    public static Boolean finished = false;

    // 回调函数
    public static BestOrderCallBack bestOrderCallBack = null;


    // 用来发送请求，接收返回数据，进行页面跳转的线程
    public static class GetPendingOrder implements Runnable {

        // 用来进行页面跳转
        private Context fromContext;
        private Class toClass;

        public GetPendingOrder(Context fromContext, Class toClass) {
            this.fromContext = fromContext;
            this.toClass = toClass;
        }

        @Override
        public void run() {
            // 轻微沉睡，保证司机状态被传送到
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 存入订单id
            Map<String, String> map = new HashMap<>();
            map.put("phone", LocalStorage.phone);

            // 使用while循环不断向后端发送订单状态获取信息
            while (true) {
                // 发起请求获取最适合订单
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "order", "getBestOrder");

                // 接收最适合订单
                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                // 获取失败可能是因为当前没有订单
                if (dataResponse.getCode().equals("200")) {
                    // 配置合适订单
                    Gson gson = new Gson();
                    LocalStorage.currentOrder = gson.fromJson(dataResponse.getData().get("order").toString(), CompletedOrderDto.class);
                    // 转换时间格式
                    LocalStorage.currentOrder = TimeUtil.setRightTimeFormat(LocalStorage.currentOrder);
                    // 设置当前订单
                    LocalStorage.currentOrderId = LocalStorage.currentOrder.getId().toString();

                    // 提示用户
                    Looper.prepare();
                    Toast.makeText(fromContext, "系统已为您推荐合适订单~", Toast.LENGTH_SHORT).show();

                    // 获取成功，跳出循环
                    break;
                } else if (dataResponse.getCode().equals("900")) {
                    // 司机取消叫单
                    break;
                }

                if (finished) {
                    break;
                }

                // 间歇800ms发一次
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            if (!finished) { // 司机没有取消出车
                // 使用接口展示订单信息
                if (bestOrderCallBack != null) {
                    bestOrderCallBack.bestOrderCallback(LocalStorage.currentOrder);
                }
            }

            // 销毁线程
            Thread.currentThread().interrupt();
        }
    }
}
